<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/6/23 0023
 * Time: 16:45
 */

namespace app\currency\controller;


use app\currency\service\TimeSlotService;
use library\Controller;
use library\tools\Data;
use think\Db;

/**
 * 保洁员地图筛选
 * Class Map
 * @package app\currency\controller
 */
class Map extends Controller
{
    protected $table = 'Cleaner';

    /**
     * 根据地图查看保洁员
     * @menu true
     * @auth true
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function index()
    {
        $this->_query($this->table)
            ->field('id,phone,name,work_headimg,address,lat,lng')
            ->where([['lat','neq',''],['lng','neq','']])
            ->order('id desc')
            ->page(false);
    }
    protected function _index_page_filter(&$data)
    {
        //根据当前订单重新计算位置和工作状态
        foreach ($data as &$datum) {
            //查询是否在工作中
            $time_slot = TimeSlotService::getCurrentTimeSlot();
            if($time_slot > 0)
            {
                $order = Db::name('CleanOrder')
                    ->where([
                        ['cid','eq',$datum['id']],
                        ['service_date','eq',date('Y-m-d')],
                        ['time_slot','eq',$time_slot],
                        ['status','gt','1']
                    ])
                    ->find();
                if($order){
                    $datum['work_state'] = true;
                    //查询地址信息
                    $center = Db::name('Cities')->where('id',$order['cities_id'])->value('cities_center');
                    list($lng,$lat) = explode(',',$center);
                    $datum['lat'] = $lat;
                    $datum['lng'] = $lng;
                }
            }
        }
        $data = json_encode($data,JSON_UNESCAPED_UNICODE);
    }

    /**
     * 分配订单
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function distribute()
    {
        $this->_form($this->table,'form');
    }
    protected function _form_filter(&$data)
    {

        if($this->request->isPost()) {
            if(!isset($data['order_no']) || $data['order_no'] == '')
            {
                $this->error('请输入订单号！');
            }
            $where = [
                ['is_deleted','eq','0'],
                ['order_no','eq',$data['order_no']],
                ['status','eq','1'],
                ['appoint_cid','eq','0']
            ];
            $order = Db::name('CleanOrder')
                ->where($where)
                ->find();
            empty($order) && $this->error('请输入正确的待接单订单号！');
            // 修改订单信息
            if(Db::name('CleanOrder')
                ->where($where)
                ->update([
                    'cid' => $data['id'],
                    'status' => '2'
                ])){
                $this->success('分配订单成功！');
            } else {
                $this->error('操作失败！');
            }
        }
    }
}